home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / BSP Tree 1.2 / Sources / Graphics / source / matrix_3d.cp < prev    next >
Encoding:
Text File  |  1995-03-25  |  9.2 KB  |  146 lines  |  [TEXT/MMCC]

  1. //------------------------------------------------------------------------------
  2. //    File:                    matrix_3d.cp
  3. //    Date:                    9/2/94
  4. //    Author:                Bretton Wade
  5. //
  6. //    Description:    this file contains methods for the matrix_3d class
  7. //
  8. //------------------------------------------------------------------------------
  9.  
  10. #include "utility.h"
  11. #include "matrix_3d.h"
  12.  
  13. //------------------------------------------------------------------------------
  14. //    global variables
  15. //------------------------------------------------------------------------------
  16. real    matrix_3d::identity[4][4] = {{R(1.0), R(0.0), R(0.0), R(0.0)},                        //    array for identity matrix_3d
  17.                                                                     {R(0.0), R(1.0), R(0.0), R(0.0)},
  18.                                                                     {R(0.0), R(0.0), R(1.0), R(0.0)},
  19.                                                                     {R(0.0), R(0.0), R(0.0), R(1.0)}};
  20.  
  21. matrix_3d    IDENTITY_MATRIX (matrix_3d::identity);                                                                //    an identity matrix_3d
  22.  
  23. //------------------------------------------------------------------------------
  24. //    constructor
  25. //------------------------------------------------------------------------------
  26. matrix_3d::matrix_3d (matrix_3d &m)                                                                                            //    copy constructor
  27. {                                                                                                                                                                //    begin
  28.     for (short i = 0; i < 4; i++)                                                                                                    //    loop on the rows
  29.         for (short j = 0; j < 4; j++)                                                                                                //    loop on the columns
  30.             mat[i][j] = m.mat[i][j];                                                                                                    //    copy the matrix_3d values
  31. }                                                                                                                                                                //    end
  32.  
  33. //------------------------------------------------------------------------------
  34. //    constructor
  35. //------------------------------------------------------------------------------
  36. matrix_3d::matrix_3d (real f[4][4])                                                                                            //    direct assignment constructor
  37. {                                                                                                                                                                //    begin
  38.     for (short i = 0; i < 4; i++)                                                                                                    //    loop on the rows
  39.         for (short j = 0; j < 4; j++)                                                                                                //    loop on the columns
  40.             mat[i][j] = f[i][j];                                                                                                            //    copy the values
  41. }                                                                                                                                                                //    end
  42.  
  43. //------------------------------------------------------------------------------
  44. //    assignment
  45. //------------------------------------------------------------------------------
  46. void    matrix_3d::operator = (const matrix_3d &m)                                                                //    assignment operator
  47. {                                                                                                                                                                //    begin
  48.     for (short i = 0; i < 4; i++)                                                                                                    //    loop on the rows
  49.         for (short j = 0; j < 4; j++)                                                                                                //    loop on the columns
  50.             mat[i][j] = m.mat[i][j];                                                                                                    //    copy the other matrx values
  51. }                                                                                                                                                                //    end
  52.  
  53. //------------------------------------------------------------------------------
  54. //    multiplication
  55. //------------------------------------------------------------------------------
  56. matrix_3d    matrix_3d::operator * (const matrix_3d &m) const                                            //    multiplication operator
  57. {                                                                                                                                                                //    begin
  58.     matrix_3d result;                                                                                                                            //    a new matrix_3d
  59.     for (short i = 0; i < 4; i++)                                                                                                    //    loop on the rows
  60.         for (short j = 0; j < 4; j++)                                                                                                //    loop on the columns
  61.         {                                                                                                                                                        //    begin
  62.             tuple_3d    r (mat[i][0], mat[i][1], mat[i][2], mat[i][3]),                                    //    the ith roww
  63.                         c (m.mat[0][j], m.mat[1][j], m.mat[2][j], m.mat[3][j]);                            //    the jth column
  64.             result.mat[i][j] = r | c;                                                                                                    //    the inner product
  65.         }                                                                                                                                                        //    end
  66.     return result;                                                                                                                                //    return the result
  67. }                                                                                                                                                                //    end
  68.  
  69. //------------------------------------------------------------------------------
  70. //    compute cofactor of 3x3 matrix_3d reduced from entry (i, j)
  71. //------------------------------------------------------------------------------
  72. real    matrix_3d::Cofactor (int i, int j) const                                                                    //    compute the cofactor for the given entry
  73. {                                                                                                                                                                //    begin
  74.     int        i0, i1, i2, j0, j1, j2;                                                                                                    //    indexing values
  75.     real    det;                                                                                                                                        //    determinant of the sub-matrix_3d (the cofactor)
  76.     switch (i)                                                                                                                                        //    switch on the desired row
  77.     {                                                                                                                                                            //    begin
  78.         case 0:    i0 = 1; i1 = 2; i2 = 3; break;                                                                            //    set appropriate indexing values
  79.         case 1:    i0 = 0; i1 = 2; i2 = 3; break;                                                                            //    set appropriate indexing values
  80.         case 2:    i0 = 0; i1 = 1; i2 = 3; break;                                                                            //    set appropriate indexing values
  81.         case 3:    i0 = 0; i1 = 1; i2 = 2; break;                                                                            //    set appropriate indexing values
  82.     }                                                                                                                                                            //    end
  83.     switch (j)                                                                                                                                        //    switch on the desired column
  84.     {                                                                                                                                                            //    begin
  85.         case 0:    j0 = 1; j1 = 2; j2 = 3; break;                                                                            //    set appropriate indexing values
  86.         case 1:    j0 = 0; j1 = 2; j2 = 3; break;                                                                            //    set appropriate indexing values
  87.         case 2:    j0 = 0; j1 = 1; j2 = 3; break;                                                                            //    set appropriate indexing values
  88.         case 3:    j0 = 0; j1 = 1; j2 = 2; break;                                                                            //    set appropriate indexing values
  89.     }                                                                                                                                                            //    end
  90.     //    compute the determinent of the resulting 3x3 matrix_3d
  91.     det =    mat[i0][j0] * (mat[i1][j1] * mat[i2][j2] - mat[i1][j2] * mat[i2][j1]) -
  92.                 mat[i0][j1] * (mat[i1][j0] * mat[i2][j2] - mat[i1][j2] * mat[i2][j0]) +
  93.                 mat[i0][j2] * (mat[i1][j0] * mat[i2][j1] - mat[i1][j1] * mat[i2][j0]);
  94.     return odd (i + j) ? -det : det;                                                                                             //    if the entry index is odd, the cofactor is inverted
  95. }                                                                                                                                                                //    end
  96.  
  97. //------------------------------------------------------------------------------
  98. //    determinant of the 4x4 matrix_3d
  99. //------------------------------------------------------------------------------
  100. real    matrix_3d::Determinant (void) const                                                                                //    compute the matrix_3d determinant
  101. {                                                                                                                                                                //    begin
  102.     return     mat[0][0] * Cofactor (0, 0) +                                                                                    //    multiply the value by its cofactor
  103.                     mat[0][1] * Cofactor (0, 1) +                                                                                    //    multiply the value by its cofactor and add the result to the current sum
  104.                     mat[0][2] * Cofactor (0, 2) +                                                                                    //    multiply the value by its cofactor and add the result to the current sum
  105.                     mat[0][3] * Cofactor (0, 3);                                                                                    //    multiply the value by its cofactor and add the result to the current sum
  106. }                                                                                                                                                                //    end
  107.  
  108. //------------------------------------------------------------------------------
  109. //    invert the matrix_3d
  110. //------------------------------------------------------------------------------
  111. matrix_3d    matrix_3d::Inverse (void) const                                                                                //    compute the matrix_3d inverse
  112. {                                                                                                                                                                //    begin
  113.     matrix_3d    inverse;                                                                                                                        //    new matrix_3d
  114.     real    det = Determinant ();                                                                                                        //    compute the determinant
  115.     for (short i = 0; i < 4; i++)                                                                                                    //    loop on the rows
  116.         for (short j = 0; j < 4; j++)                                                                                                //    loop on the columns
  117.             inverse.mat[j][i] = Cofactor (i, j) / det;                                                                //    compute the inverse value for the current entry
  118.     return inverse;                                                                                                                                //    return the matrix_3d
  119. }                                                                                                                                                                //    end
  120.  
  121. //------------------------------------------------------------------------------
  122. //    pre-multiplication by a tuple_3d
  123. //------------------------------------------------------------------------------
  124. tuple_3d    operator * (const tuple_3d &t, const matrix_3d &m)                                        //    multiplication operator
  125. {                                                                                                                                                                //    begin
  126.     tuple_3d    c0 (m(0, 0), m(1, 0), m(2, 0), m(3, 0)),                                                        //    build the vector_3d for the first column
  127.                 c1 (m(0, 1), m(1, 1), m(2, 1), m(3, 1)),                                                                //    build the vector_3d for the second column
  128.                 c2 (m(0, 2), m(1, 2), m(2, 2), m(3, 2)),                                                                //    build the vector_3d for the third column
  129.                 c3 (m(0, 3), m(1, 3), m(2, 3), m(3, 3));                                                                //    build the vector_3d for the fourth column
  130.     return tuple_3d (t | c0, t | c1, t | c2, t | c3);                                                            //    return tuple_3d composed of inner products
  131. }                                                                                                                                                                //    end
  132.  
  133. //------------------------------------------------------------------------------
  134. //    post-multiplication by a tuple_3d
  135. //------------------------------------------------------------------------------
  136. tuple_3d    operator * (const matrix_3d &m, const tuple_3d &t)                                        //    multiplication operator
  137. {                                                                                                                                                                //    begin
  138.     tuple_3d    r0 (m(0, 0), m(0, 1), m(0, 2), m(0, 3)),                                                        //    build the vector_3d for the first column
  139.                 r1 (m(1, 0), m(1, 1), m(1, 2), m(1, 3)),                                                                //    build the vector_3d for the second column
  140.                 r2 (m(2, 0), m(2, 1), m(2, 2), m(2, 3)),                                                                //    build the vector_3d for the third column
  141.                 r3 (m(3, 0), m(3, 1), m(3, 2), m(3, 3));                                                                //    build the vector_3d for the fourth column
  142.     return tuple_3d (t | r0, t | r1, t | r2, t | r3);                                                            //    return tuple_3d composed of inner products
  143. }                                                                                                                                                                //    end
  144.  
  145. //------------------------------------------------------------------
  146.